package com.kzd.btgame.main.game

import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.viewpager.widget.ViewPager
import com.google.android.material.tabs.TabLayout
import com.kzd.btgame.R
import com.kzd.btgame.databinding.ActivityHomeTravelBinding
import com.kzd.btgame.entity.TravelTab
import com.kzd.btgame.main.KEY_HOME_TRAVEL_INDEX
import com.module.lemlin.app.OwnerStatusBar
import com.module.lemlin.owner.OwnerViewBindingActivity
import java.lang.Exception

class GameTravelActivity : OwnerViewBindingActivity<ActivityHomeTravelBinding>() {

    private val mTravelTabs = mutableListOf(
        TravelTab(R.mipmap.newgame_icon, name = "今日首发", type = TravelTab.Type.TODAY),
        TravelTab(R.mipmap.weektop_icon, name = "一周新游Top10", type = TravelTab.Type.WEEK),
        TravelTab(R.mipmap.newgame_yy_icon, name = "新游预约", type = TravelTab.Type.RESERVE)
    )

    private val mIndex: Int
        get() = try {
            (intent?.extras?.getString(KEY_HOME_TRAVEL_INDEX) ?: "0").toInt()
        } catch (e: Exception) {
            0
        }

    override val inflate: (LayoutInflater) -> ActivityHomeTravelBinding
        get() = ActivityHomeTravelBinding::inflate

    override fun initStatusBar(statusBar: OwnerStatusBar) {
        statusBar.run {
            addView = mViewBinding.flHomeTravelBar
        }
        super.initStatusBar(statusBar)
    }

    override fun initView() {

        val pagerAdapter = object : FragmentStatePagerAdapter(
            supportFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
        ) {
            override fun getItem(position: Int): Fragment =
                GameTravelFragment.newInstance(mTravelTabs[position])

            override fun getCount(): Int = mTravelTabs.size

            override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {}
        }
        mViewBinding.vpHomeTravelBody.adapter = pagerAdapter

        mViewBinding.tabHomeTravelKind.apply {
            for ((index: Int, item: TravelTab) in mTravelTabs.withIndex()) {
                val view = LayoutInflater.from(context)
                    .inflate(R.layout.item_travel_tab, null)
                view.findViewById<ImageView>(R.id.ivTravelTabIcon)
                    .setImageResource(item.resId)
                view.findViewById<TextView>(R.id.tvTravelTabName)
                    .text = item.name
                val tab = newTab().apply {
                    customView = view
                    tag = index
                }
                addTab(tab)
            }
        }



        mViewBinding.tabHomeTravelKind.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
            override fun onTabReselected(tab: TabLayout.Tab?) = Unit

            override fun onTabUnselected(tab: TabLayout.Tab?) = Unit

            override fun onTabSelected(tab: TabLayout.Tab?) {
                mViewBinding.vpHomeTravelBody.currentItem = tab?.position ?: 0
            }
        })

        mViewBinding.vpHomeTravelBody.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) = Unit

            override fun onPageScrolled(
                position: Int, positionOffset: Float, positionOffsetPixels: Int,
            ) = Unit

            override fun onPageSelected(position: Int) {
                mViewBinding.tabHomeTravelKind.getTabAt(position)?.select()
            }
        })

        mViewBinding.tabHomeTravelKind.getTabAt(mIndex)?.select()

    }

}